package com.komon.constant;

import java.lang.reflect.Field;
import java.util.Map;

import com.komon.constant.header.EntityHeader;
import com.komon.constant.header.GeneralHeader;
import com.komon.constant.header.RequestHeader;

public class HttpHeader {
	private GeneralHeader generalHeader;
	private EntityHeader entityHeader;
	private RequestHeader requestHeader;
	
	public HttpHeader (Map<String, String> headers) {
		populateHeader(headers, generalHeader);
		populateHeader(headers, entityHeader);
	}
	
	//TODO need be refactor
	public static void populateHeader(Map<String,String> headers, Object header) {
		for (Field f : header.getClass().getDeclaredFields()) {
			f.setAccessible(true);
			String fName = f.getName();
			for (Map.Entry<String, String> entry : headers.entrySet()) {
				//TODO
				if (match(f.getName(), entry.getKey())) {
					try {
						f.set(header, entry.getValue());
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
	
	public static boolean match(String komonName, String httpName){
		return httpName.replaceAll("-", "_").equalsIgnoreCase(komonName);
	}
	
	public static  String getHeaderValueFromKomonMap(String httpHeaderName, Map<String, String> komonHeaderMap) {
		for (Map.Entry<String, String> entry : komonHeaderMap.entrySet()) {
			if (match( entry.getKey(), httpHeaderName)){
				return entry.getValue();
			}
		}
		return null;
	}
	
}
